package cn.edu.nuc.onlinestore.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;
import javax.swing.table.TableModel;

import cn.edu.nuc.onlinestore.util.Acceptutil_2;
import cn.edu.nuc.onlinestore.util.GoodsUpdateutil;
import cn.edu.nuc.onlinestore.vo.Goods;

import javax.swing.JScrollBar;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.GridLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.ImageIcon;

public class AdminStore extends JFrame {

	private JPanel contentPane;
	private JTextField selectText;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminStore frame = new AdminStore("");
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AdminStore(String name) {
		
		
		String sname="";
		Acceptutil_2 ac2=new Acceptutil_2();
		ac2.start();
				
		List<Goods> list=new ArrayList<Goods>();
		setTitle("中北商场后台管理系统--当前用户: "+name);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 732, 467);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(10, 78, 696, 341);
		panel.setLayout(new GridLayout(1, 1, 0, 0));
		
		DefaultTableModel model = new DefaultTableModel();
		model.addColumn("商品编号");
		model.addColumn("名称");
		model.addColumn("单价(人民币)");
		model.addColumn("库存");
		//model.addColumn("操作");
		if(sname.equals("")){
				File f=new File("D:\\store\\goods\\");
				File[] f1=f.listFiles();
				for(int i=0;i<f1.length;i++){
					GoodsUpdateutil gu=new GoodsUpdateutil();
					Goods g=gu.ObjectUtil_a(f1[i]);
					list.add(g);
					model.addRow(new String[]{String.valueOf(g.getId()),g.getName(),String.valueOf(g.getPrice()),String.valueOf(g.getNum())});
				}
		
		}
//		model.addRow(new String[]{"1","水杯","15.00","200"});
//		model.addRow(new String[]{"2","水瓶","35.00","200"});
//		model.addRow(new String[]{"3","天堂伞","55.00","200"});
//		model.addRow(new String[]{"4","男袜","8.00","200"});
//		model.addRow(new String[]{"5","农夫山泉","2.00","200"});
//		model.addRow(new String[]{"6","毛巾","9.90","200"});
//		model.addRow(new String[]{"7","牙刷","15.00","200"});
//		model.addRow(new String[]{"8","洗发水","15.00","200"});
//		model.addRow(new String[]{"9","牙膏","15.00","200"});
//		model.addRow(new String[]{"10","海尔全自动洗衣机","2,699.00","200"});
		JTable table = new JTable( model );
		
		JScrollPane pane = new JScrollPane( table );
		
		panel.add(pane);
		contentPane.add(panel);
		
		JButton button = new JButton("添加商品");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				AdminAdd add = new AdminAdd();
				add.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				add.setVisible(true);
			}
		});
		button.setBounds(386, 45, 93, 23);
		contentPane.add(button);
		
		JButton button_1 = new JButton("修改商品");
		button_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				int selectRow=table.getSelectedRows().length;
				System.out.println(selectRow);

				DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

				if(selectRow==1){
				  int selectedRowIndex = table.getSelectedRow(); // 取得用户所选单行 
				  Goods g1=list.get(selectedRowIndex);
				  System.out.println(g1);
				  
				  AdminUpdate u = new AdminUpdate(g1);
				  u.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
				  u.setVisible(true);
				 
				}else{
					JOptionPane.showMessageDialog(null, "请选择单行内容", "alert", JOptionPane.ERROR_MESSAGE); 
				}
				
				
				
			}
		});
		button_1.setBounds(489, 45, 93, 23);
		contentPane.add(button_1);
	/*
	 * 删除部分	
	 */
		JButton button_2 = new JButton("删除选中商品");
		button_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//得到当前选中商品项
				int a=JOptionPane.showConfirmDialog(null, "确定要删除\"水杯\"么?" );
				System.out.println(a);
				if(a==0){
					int selectRow=table.getSelectedRows().length;
					//System.out.println(selectRow);

					DefaultTableModel tableModel = (DefaultTableModel) table.getModel();

					if(selectRow==1){
					  int selectedRowIndex = table.getSelectedRow(); // 取得用户所选单行 
					  
					  String name=(String)tableModel.getValueAt(selectedRowIndex, 1);
					 System.out.println(name);
					 GoodsUpdateutil gu=new GoodsUpdateutil();
					 
					 if( gu.Remove(name)){
						 JOptionPane.showMessageDialog(null, "删除成功", "alert", JOptionPane.ERROR_MESSAGE); 
					 }else{
						 JOptionPane.showMessageDialog(null, "删除失败", "alert", JOptionPane.ERROR_MESSAGE); 
					 }
					}else{
						JOptionPane.showMessageDialog(null, "请选择单行内容", "alert", JOptionPane.ERROR_MESSAGE); 
					}
				}
			}
		});
		button_2.setBounds(587, 45, 119, 23);
		contentPane.add(button_2);
		
		JButton button_3 = new JButton("退出登录");
		button_3.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				File f4=new File("D:/store/Online/admin/");
				File[] f4_1=f4.listFiles();
				for(int i=0;i<f4_1.length;i++){
					System.out.println(f4_1[i].delete());
				}
				File f3=new File("D:/store/Online/admin/");
				
				if(f3.length()==0){
					
					File f=new File("D:/store/Online/user/");
					File[] f1=f.listFiles();
					for(int i=0;i<f1.length;i++){
						System.out.println(f1[i].delete());
					}
					JOptionPane.showMessageDialog(null, "服务端关闭，所有用户将下线", "alert", JOptionPane.ERROR_MESSAGE); 
					System.exit(0);
				}
			}
		});
		button_3.setBounds(613, 6, 93, 23);
		contentPane.add(button_3);
		
		JLabel OnlineLabel = new JLabel("当前在线用户数: 5");
		OnlineLabel.setBounds(10, 10, 162, 15);
		contentPane.add(OnlineLabel);
		
		JLabel lblid = new JLabel("商品名称:");
		lblid.setBounds(10, 53, 54, 15);
		contentPane.add(lblid);
		
		selectText = new JTextField();
		selectText.setBounds(68, 50, 104, 21);
		contentPane.add(selectText);
		selectText.setColumns(10);
/**
 * 搜索部分		
 */
		JButton button_4 = new JButton("搜索");
		button_4.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				//model.getDataVector().clear();
				model.setRowCount(0);
				if(selectText.getText().equals("")){
					File f=new File("D:\\store\\goods\\");
					File[] f1=f.listFiles();
					for(int i=0;i<f1.length;i++){
						GoodsUpdateutil gu=new GoodsUpdateutil();
						Goods g=gu.ObjectUtil_a(f1[i]);
						//list.add(g);
						model.addRow(new String[]{String.valueOf(g.getId()),g.getName(),String.valueOf(g.getPrice()),String.valueOf(g.getNum())});
					}
			}else{
				    File f=new File("D:\\store\\goods\\");
				    File[] f1=f.listFiles();
				    for(int i=0;i<f1.length;i++){
				    	GoodsUpdateutil gu=new GoodsUpdateutil();
				    	Goods g=gu.ObjectUtil_a(f1[i]);
				    	list.add(g);
				    	if(g.getName().equals(selectText.getText())){
				    		model.addRow(new String[]{String.valueOf(g.getId()),g.getName(),String.valueOf(g.getPrice()),String.valueOf(g.getNum())});
				    	}
				    	
				    }
			}
				
				
               
			}
		});
		button_4.setBounds(180, 49, 93, 23);
		contentPane.add(button_4);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon(AdminStore.class.getResource("/image/be6bda5a2c70d859a4483d559811ca38.jpg")));
		lblNewLabel.setBounds(0, 0, 716, 429);
		contentPane.add(lblNewLabel);
		
		new Thread(new Runnable(){

			@Override
			public void run() {
				// TODO Auto-generated method stub
				while(true){
					File f=new File("D:/store/Online/user");
					int b=f.list().length;
					OnlineLabel.setText("当前在线用户数: "+String.valueOf(b));
				}
		}}).start();
	}
}
